/*
 * CastDRAndroid
 * Copyright (c) 2014 Christian Holm Christensen
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 *
 */
package org.cholm.media.castdr.adapters;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import org.cholm.media.castdr.LogUtils;
import org.cholm.media.castdr.R;
import org.cholm.media.castdr.backend.Category;

import java.util.ArrayList;
import java.util.List;

import static org.cholm.media.castdr.LogUtils.LOGd;
/**
 * This is the adapter to use for the drawer - if any
 *
 * @author cholm
 */
public class DrawerAdapter extends BaseAdapter {
    /**
     * List of holders
     */
    private static final String TAG = LogUtils.makeLogTag(DrawerAdapter.class);
    public List<Holder> mHolders = new ArrayList<Holder>();
    protected List<Category> mData = null;
    protected Context mContext = null;
    protected Settings mSettings = null;

    /**
     *
     */
    public DrawerAdapter(Context ctx) {
        mContext = ctx;
        mSettings = new Settings(mContext);
    }

    /*
     * (non-Javadoc)
     *
     * @see android.widget.Adapter#getCount()
     */
    @Override
    public int getCount() {
        if (mData == null) return 0;
        return mData.size();
    }

    /*
     * (non-Javadoc)
     *
     * @see android.widget.Adapter#getItem(int)
     */
    @Override
    public Object getItem(int position) {
        return getCategory(position);
    }

    /*
     * (non-Javadoc)
     *
     * @see android.widget.Adapter#getItemId(int)
     */
    @Override
    public long getItemId(int position) {
        return position;
    }

    /*
     * (non-Javadoc)
     *
     * @see android.widget.Adapter#getView(int, android.view.View,
     * android.view.ViewGroup)
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Category c = getCategory(position);

        if (c == null) return null;
        Holder holder = null;
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) this.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.category_entry, null);
            holder = new Holder(convertView);
        }
        if (holder == null) holder = (Holder) convertView.getTag();
        holder.set(c);

        return convertView;
    }

    /**
     * Get category at a position
     *
     * @param position Position to look at
     * @return Category object or null
     */
    public Category getCategory(int position) {
        // Exclude the search category
        int n = getCount();
        if (mData == null || position < 0 || position >= n) {
            return null;
        }
        Category r = null;
        if (position < mData.size()-1) {
            r = mData.get(position);
        }
        else r = mSettings;
        LOGd(TAG, "Returning category @ " + position + ": " + r);
        if (r != null) {
            LOGd(TAG, "Category " + r.getName() + " [" + r.getClass()
                    .getSimpleName() + "]");
        }
        return r;
    }
    public Category getSettings() { return mSettings; }

    public void setData(List<Category> data) {
        mData = data;
    }

    /**
     * Dummy category for settings
     */
    public class Settings extends Category {
        public Settings(Context context) {
            super(context.getResources().getString(R.string
                    .title_activity_settings));
        }
    }
    /**
     * Holder class
     */
    public class Holder {
        /**
         * Default background
         */
        public static final int DEFAULT_BG = Color.TRANSPARENT;
        /**
         * Top view
         */
        public View mTopView = null;
        /**
         * Text view
         */
        public TextView mTextView = null;
        /**
         * Whether this is selected or not
         */
        public boolean mSelected = false;

        /**
         * Constructor
         *
         * @param v  View this is a holder for
         */
        public Holder(View v) {
            mTopView = v;
            mTextView = (TextView) v.findViewById(R.id.group_name);
            v.setTag(this);
            mHolders.add(this);
        }

        public void set(Category c) {
            mTextView.setText(c.getName());
            update(false);
        }

        /*
         * (non-Javadoc)
         *
         * @see java.lang.Object#toString()
         */
        public String toString() {
            return "" + mTextView.getText();
        }

        /**
         * Update the display
         *
         * @param selected If true, show as selected
         */
        public void update(boolean selected) {
            mSelected = selected;
            if (!mSelected) { // was selected
                mTopView.setBackgroundColor(DEFAULT_BG);
                mTextView.setTextColor(Color.BLACK);
            } else {
                mTopView.setBackgroundColor(mContext.getResources()
                        .getColor(R.color.drorange));
                mTextView.setTextColor(Color.WHITE);
            }
            if (!selected) return;
            for (Holder h : mHolders) {
                if (h == this) continue;
                h.update(false);
            }
        }
    }
}
